﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{

    public partial class UITMKRF:ViewBase
    { 
        
        public override void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e); 
        }
        public override void vwMaster_CurrentChanged(object sender, System.EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwMKRF = dwMaster;
            if (dwMKRF != null)
            {
                KEY = dwMKRF["ABAID"].ToString();
                JOBID = dwMKRF["ABAID"].ToString();
            }
            else
            {
                KEY = "";
                JOBID = "";
            }
        } 
        public virtual void AMMontitorCommand(object sender, RoutedEventArgs e)
        {
            TMV.StartMonitoring();
        }
        public virtual void OnShowWJobChg(bool show)
        {
            
        }

        public virtual void OnShowAllChg(bool show)
        {
           
        }
       
        public virtual void SetupMachine()
        {
            int count = STKZoneCalendar.Children.Count;
            STKZoneCalendar.Children.RemoveRange(0, count);
            Scheduler.SetEnv.Clear(); 
            Scheduler.SetEnv.Merge(TMV.LOADMACHINE(KSTKRS, KST, AMTYP));  
            Scheduler.GDensity = DENSITY;
            Scheduler.GDays = GDays;
            Scheduler.HistoryDays = HistoryDays;
            Scheduler.Pagescope = pagescope;
            Scheduler.MachinePanel = STKMachineTable;
            Scheduler.GanttPanel = STKmachines;
            Scheduler.ZoneCalendar = STKZoneCalendar; 
            Scheduler.DrawCalendar();
            Scheduler.DrawMachine(true); 
        } 
        public virtual void UpdateCalendarCommand(object sender, RoutedEventArgs e)
        {
            NormalTask =System.Threading.Tasks.Task.Factory.StartNew(() => SetupMachine(), new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
        } 
        public override void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            base.bgWorker_RunWorkerCompleted(sender, e);
            vwMaster.View.MoveCurrentTo(-1);
            EventDispatcher.Instance.RaiseStatus("数据查询完成", StatusEventType.StopProgress);
        }

        public override void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            base.bgWorker_DoWork(sender, e); 

        }
         
        public void RemoveAllJobs()
        {
            foreach (UIAM machine in MachinesLib.Values)
            {
                int count = machine.MGANTT.MainGrid.Children.Count;
                if (count > 1)
                {
                    machine.MGANTT.MainGrid.Children.RemoveRange(1, count - 1);
                }
            }
        } 

        public void FocusJob(AniScrollViewer viewer, StackPanel master, UIJOB job)
        {
             
            job.IsHit = true;
            Lastjob = job;
            /*
            job.Background = Brushes.Wheat;
            DropShadowEffect effect = new DropShadowEffect();
            effect.BlurRadius = 2;
            effect.Color = Color.FromRgb(255, 255, 0);
            job.Effect = effect;
            RotateTransform rt = new RotateTransform();
            rt.Angle = 0;
            TransformGroup tg = new TransformGroup();
            tg.Children.Add(rt);
            job.RenderTransform = tg;
            */
            MGANTT MGANTT = (MGANTT)this.FindName(job.MGANTT.Name);
            Grid MainGrid = MGANTT.MainGrid;
            Point relativePoint = job.TransformToAncestor(MainGrid).Transform(new Point(0, 0));
            double x = relativePoint.X;
            relativePoint = MGANTT.TransformToAncestor(master).Transform(new Point(0, 0));
            double y = (relativePoint.Y);
            ScrollToPosition(viewer, x, y);
        }

        public void FocusMachine(AniScrollViewer viewer, StackPanel master, UIAM job)
        {

            job.Background = Brushes.Wheat;
            DropShadowEffect effect = new DropShadowEffect();
            effect.BlurRadius = 10;
            effect.Color = Color.FromRgb(255, 255, 0);
            job.Effect = effect;
            RotateTransform rt = new RotateTransform();
            rt.Angle = -2;
            TransformGroup tg = new TransformGroup();
            tg.Children.Add(rt);
            job.RenderTransform = tg;

            Point relativePoint = job.TransformToAncestor(master).Transform(new Point(0, 0));
            ScrollToPosition(viewer, relativePoint.X, relativePoint.Y);
        }

        public void UnFocusJob(UIJOB job)
        {
            job.Background = Brushes.White;
            job.Effect = null;
            job.RenderTransform = null;
            job.IsHit = false;
        }
      
        public void ScrollToPosition(AniScrollViewer viewer, double x, double y)
        {
            //vwCalendar.ScrollToHorizontalOffset(x);
            //vwMachine.ScrollToVerticalOffset(y);
            DoubleAnimation vertAnim = new DoubleAnimation();
            vertAnim.From = viewer.VerticalOffset;
            vertAnim.To = y;
            vertAnim.DecelerationRatio = .2;
            vertAnim.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
            DoubleAnimation horzAnim = new DoubleAnimation();
            horzAnim.From = viewer.HorizontalOffset;
            horzAnim.To = x;
            horzAnim.DecelerationRatio = .2;
            horzAnim.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
            Storyboard sb = new Storyboard();
            sb.Children.Add(vertAnim);
            sb.Children.Add(horzAnim);
            Storyboard.SetTarget(vertAnim, viewer);
            //Storyboard.SetTarget(vwCalendar, viewer);
            Storyboard.SetTargetProperty(vertAnim, new PropertyPath(AniScrollViewer.CurrentVerticalOffsetProperty));
            Storyboard.SetTarget(horzAnim, viewer);
            //Storyboard.SetTarget(vwMachine, viewer);
            Storyboard.SetTargetProperty(horzAnim, new PropertyPath(AniScrollViewer.CurrentHorizontalOffsetProperty));
            sb.Begin();
        }
        public bool LastShow = false;
        public virtual void OnShowGanttChg(bool show)
        {
            

        }
        public virtual void btnShowAllJob_Click(object sender, RoutedEventArgs e)
        {
            cvMaster.CustomFilter = "JOBTYPE<>'X'";
        }

        public virtual void btnJobFilter_Click(object sender, RoutedEventArgs e)
        {
            string filter = "ABFAUFKRS='" + KRS + "' AND ABFAUFLFD='" + LFD + "'";
            cvMaster.CustomFilter = filter;
        }
        public virtual void TodayStartCommand(object sender, RoutedEventArgs e)
        {
            string filter = "ABFISTARTDAT='" + TMV.date + "'";
            cvMaster.CustomFilter = filter;
        }

        public virtual void OnJOBIDChg(string value)
        {
           
            
        }
      
        

        public void vwMachine_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            //mainViewer.ScrollToVerticalOffset(vwMachine.VerticalOffset);         
        }

     
        public void ShowWithJobsOnlyCommand(object sender, RoutedEventArgs e)
        {
            ShowWJobs = true;
        }

        public void ShowAllMachineCommand(object sender, RoutedEventArgs e)
        {
            ShowAllMachine = true;
        }
        public void ZoneAdjustCommand(object sender, RoutedEventArgs e)
        {
            if (zAV33Height < 100)
            {
                zAV33Height = 400;
            }

            else
            {
                zAV33Height = zAV33Height * 0.8;
            }
        }

        public void DENSITY_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.Focus();
            ComboBox cd = sender as ComboBox;
            if (cd.SelectedItem == null)
                return;
            DENSITY = Convert.ToInt16(((ComboBoxItem)cd.SelectedItem).Content);
        }

        public virtual void ConfirmScheduleCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.ConfirmSchedule(dwMaster);

        }
        public virtual void CancelScheduleCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.CancelSchedule(dwMaster);
        }
    }
}
